// pages/services/services.js
const api = require("../../comm/api")
const http = require("../../utils/http")
const util = require("../../utils/util")
import Notify from '../../components/@vant/weapp/notify/notify';

var map = {} // 服务字典

Page({

    /**
     * 页面的初始数据
     */
    data: {
        categorys: [],
        services: [],
    },

    onLoad(options) {
        this.loadServices()
    },

    // 加载服务
    loadServices() {
        http.getJSON(api.Service.List.PATH, pojo => {
            const servicesList = pojo.data
            const categorys = this.parseServices(servicesList)
            this.setData({
                categorys: categorys
            })
            this.showServices({
                detail: 0
            })
        }, err => {
            Notify({
                type: 'warning',
                message: JSON.stringify(err)
            })
        })
    },

    parseServices(servicesList) {
        map = {}
        var categorys = []
        servicesList.forEach(service => {
            const serviceType = service.serviceType
            var list = map[serviceType]
            if (!list) {
                categorys.push(serviceType)
                list = []
                map[serviceType] = list
            }
            list.push(service)
        })
        return categorys
    },

    showServices(e) {
        const index = e.detail
        const serviceType = this.data.categorys[index]
        this.setData({
            services: map[serviceType]
        })
    },

    onService(e) {
        const serviceName = e.currentTarget.dataset.name
        Notify({
            type: 'primary',
            message: serviceName + '正在上架中，请稍候...'
        })
    }
})